//
//  SYS_FootView.m
//  System_App
//
//  Created by 吴健 on 2017/11/29.
//  Copyright © 2017年 czy001. All rights reserved.
//

#import "SYS_FootView.h"
#import "SYS_OrderCell.h"
#import "SYS_OrderDetailVC.h"
#import "SYS_ShipmentVC.h"

@interface SYS_FootView ()<UITableViewDelegate, UITableViewDataSource>
{
    SYS_TableView *tbv;
    SYS_NullView *nullView;
}
@end

@implementation SYS_FootView

- (id)initWithFrame:(CGRect)frame{
    if (self == [super initWithFrame:frame]) {
        [self setUI];
    }
    return self;
}

#pragma mark -- Setter and Getter
- (void)setUI{
    tbv = [[SYS_TableView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 150) style:UITableViewStylePlain];
    tbv.delegate       = self;
    tbv.dataSource     = self;
    tbv.scrollEnabled  = NO;
    tbv.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addSubview:tbv];

    nullView = [[SYS_NullView alloc] initWithFrame:CGRectMake(0, 0, 150, 120)];
    nullView.photo.image = WJIMAGE(@"home_orderNull");
    nullView.title.text  = @"还没有任何订单喔~";
    nullView.center      = CGPointMake(tbv.center.x, tbv.center.y);
    nullView.hidden      = YES;
    [tbv addSubview:nullView];
    [self setupAutoHeightWithBottomView:tbv bottomMargin:10];
}

- (void)setDataArray:(NSArray *)dataArray{
    _dataArray = dataArray;
    [tbv reloadData];
    if (_dataArray.count > 0) {
        nullView.hidden = YES;
        tbv.sd_layout
        .leftSpaceToView(self, 0)
        .topSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .heightIs(tbv.contentSize.height);
        
    }else{
        nullView.hidden = NO;
        tbv.sd_layout
        .leftSpaceToView(self, 0)
        .topSpaceToView(self, 0)
        .rightSpaceToView(self, 0)
        .heightIs(150);
    }
//    tbv.sd_layout
//    .leftSpaceToView(self, 0)
//    .topSpaceToView(self, 0)
//    .rightSpaceToView(self, 0)
//    .heightIs(tbv.contentSize.height);

     [self setupAutoHeightWithBottomView:tbv bottomMargin:0];
    NSLog(@"++++++++%f",tbv.contentSize.height);
}

#pragma mark -- Touch and Event
- (void)doShpmentsAction:(UIButton *)sender{
    SYS_OrderListModel *model = _dataArray[sender.tag - 100];
    SYS_ShipmentVC *vc = [[SYS_ShipmentVC alloc] initWithOrderId:model.userOrderId];
    [[self viewController].navigationController pushViewController:vc animated:YES];
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SYS_OrderDetailVC *vc = [[SYS_OrderDetailVC alloc] init];
    SYS_OrderListModel *model = _dataArray[indexPath.row];
    vc.orderId = model.userOrderId;
    [[self viewController].navigationController pushViewController:vc animated:YES];
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    SYS_OrderListModel *model = _dataArray[indexPath.row];
    return 130+100*model.orderItems.count;
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"SYS_OrderCell";
    SYS_OrderCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [SYS_OrderCell getOrderCell];
    }
    SYS_OrderListModel *model = _dataArray[indexPath.row];
    cell.model = model;
    cell.btn_shipments.tag = 100+indexPath.row;
    [cell.btn_shipments addTarget:self action:@selector(doShpmentsAction:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}

#pragma mark -- Custom
//获取View所在的Viewcontroller方法
- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder *nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextResponder;
        }
    }
    return nil;
}

@end
